﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2011
 */

#region Using

using System;
using System.Data;
using System.ComponentModel;
using NetBay.Core.Log;
using NetBay.Core.DataAccess;
using Rio.Framework.RioManager;
using NetBay.Core.Extensions;
using System.Data.OracleClient;
using System.Collections.Generic;
using NetBay.Core.BusinessEntities.Connectors;

#endregion Using

namespace Rio.Framework.DataAccess
{
    /// <summary>
    /// DataAcces for Dashboard
    /// </summary>
    public class DA_Connector
    {
        #region Enums

        public enum DbFunction
        {
            [DescriptionAttribute("")]
            None,
            [DescriptionAttribute("P_ERP_CONN_Get")]
            GetConnectorInformationList,
            [DescriptionAttribute("P_ERP_CORR_Get_ByConnId")]
            GetConnectorCorrespondenceList,
            [DescriptionAttribute("P_ERP_PARAM_Get_ByConnId")]
            GetConnectorParametersList,
            [DescriptionAttribute("P_INDEX_Get_ByTableName")]
            GetConnectorFolderTypeCorrespondenceList,
            [DescriptionAttribute("P_ERP_CONN_Add")]
            AddConnector,
            [DescriptionAttribute("P_ERP_CORR_Add")]
            AddCorrespondenceConnector,
            [DescriptionAttribute("P_ERP_PARAM_Add")]
            AddParametersConnector,
            [DescriptionAttribute("P_ERP_CONN_Delete")]
            DeleteConnectorByConnectorID,
            [DescriptionAttribute("P_ERP_CONN_Update")]
            UpdateConnectorByConnectorID,
        }

        #endregion Enums

        #region Get

        /// <summary>Get connector name</summary>
        /// <returns></returns>
        public static IDataReader GetConnectorNameList()
        {
            try
            {
                //Variable declaration.
                var db = DbHelper.DataBase;

                //Log.
                NetBay.Core.Log.LogManager.Instance().LogDebug("DA_Connector.GetConnectorNameList --> No parameters");

                var dbCommand = db.GetNewCommand(DbFunction.GetConnectorInformationList.szGetDescription());

                //Execute Stored procedure.
                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Connector.GetConnectorNameList Exception [" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>Get connector information list</summary>
        /// <returns>Connector information list.</returns>
        public static IDataReader GetConnectorInformationList()
        {
            try
            {
                //Variable declaration.
                var db = DbHelper.DataBase;

                //Log.
                NetBay.Core.Log.LogManager.Instance().LogDebug("DA_Connector.GetConnectorInformationList --> No parameters");

                var dbCommand = db.GetNewCommand(DbFunction.GetConnectorInformationList.szGetDescription());

                //Execute Stored procedure.
                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Connector.GetConnectorInformationList Exception [" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>Get connector correspondence list</summary>
        /// <param name="p_lConnectorId">Connector Id.</param> 
        /// <returns>Connector correspondence list.</returns>
        public static IDataReader GetConnectorCorrespondenceList(long p_lConnectorId)
        {
            try
            {
                //Variable declaration.
                var db = DbHelper.DataBase;

                //Log.
                NetBay.Core.Log.LogManager.Instance().LogDebug("DA_Connector.GetConnectorCorrespondenceList (p_lConnectorId <" + p_lConnectorId + ">)");

                //Command declaration.
                var dbCommand = db.GetNewCommand(DbFunction.GetConnectorCorrespondenceList.szGetDescription());
                db.AddInParameter(dbCommand, "p_lErpConnId", DbType.Int64, p_lConnectorId);

                //Execute Stored procedure.
                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Connector.GetConnectorCorrespondenceList Exception [" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>Get connector parameters list</summary>
        /// <param name="p_lConnectorId">Connector Id.</param> 
        /// <returns>Connector parameters list.</returns>
        public static IDataReader GetConnectorParametersList(long p_lConnectorId)
        {
            try
            {
                //Variable declaration.
                var db = DbHelper.DataBase;

                //Log.
                NetBay.Core.Log.LogManager.Instance().LogDebug("DA_Connector.GetConnectorParametersList (p_lConnectorId <" + p_lConnectorId + ">)");

                //Command declaration.
                var dbCommand = db.GetNewCommand(DbFunction.GetConnectorParametersList.szGetDescription());
                db.AddInParameter(dbCommand, "p_lErpConnId", DbType.Int64, p_lConnectorId);

                //Execute Stored procedure.
                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Connector.GetConnectorParametersList Exception [" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>Get folder type indexes correspondence list.</summary>
        /// <param name="p_sFolderTypeTableName">Folder type table name.</param>  
        /// <returns>Folder type indexes correspondence list.</returns>
        public static IDataReader GetFolderTypeIndexCorrespondanceList(string p_sFolderTypeTableName)
        {
            try
            {
                //Variable declaration.
                var db = DbHelper.DataBase;

                //Log.
                NetBay.Core.Log.LogManager.Instance().LogDebug("DA_Connector.GetFolderTypeIndexCorrespondanceList (p_sFolderTypeTableName <" + p_sFolderTypeTableName + ">)");

                //Command declaration.
                var dbCommand = db.GetNewCommand(DbFunction.GetConnectorFolderTypeCorrespondenceList.szGetDescription());
                db.AddInParameter(dbCommand, "p_sFolderTypeTableName", DbType.String, p_sFolderTypeTableName);

                //Execute Stored procedure.
                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Connector.GetFolderTypeIndexCorrespondanceList Exception [" + ex.Message + "]");
                return null;
            }
        }

        #endregion Get

        #region Added

        /// <summary>Add RIO connector.</summary>
        /// <param name="p_sConnectorName">Connector name to add.</param>
        /// <param name="p_sConnectorDescription">Connector description to add.</param>
        /// <returns>Connector Identifiant. (recon_id)</returns>
        public static long AddConnector(string p_sConnectorName, string p_sConnectorDescription)
        {
            //Variable initialization.
            long l_lConnectorId = 0;

            try
            {
                //Variable declaration.
                var db = DbHelper.DataBase;

                //Log.
                NetBay.Core.Log.LogManager.Instance().LogDebug("DA_Connector.AddConnector (p_sConnectorName <" + p_sConnectorName + ">, p_sConnectorDescription <" + p_sConnectorDescription + ">)");

                //Command declaration.
                using (var dbCommand = db.GetNewCommand(DbFunction.AddConnector.szGetDescription()))
                {
                    db.AddInParameter(dbCommand, "p_sErpConnName", DbType.String, p_sConnectorName);
                    db.AddInParameter(dbCommand, "p_sErpConnDesc", DbType.String, p_sConnectorDescription);

                    using (IDataReader P_oDbDataReader = db.ExecuteReader(dbCommand))
                    {

                        while (P_oDbDataReader.Read())
                        {
                            l_lConnectorId = Convert.ToInt64(P_oDbDataReader["RECON_ID"]);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Connector.AddConnector Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_lConnectorId;
        }

        /// <summary>Add RIO correspondence connector.</summary>
        /// <param name="p_oConnectorCorrespondenceToAdd">Connector Correspondence Object.</param>
        /// <param name="p_lConnectorId">Connector Id.</param>
        /// <param name="p_lEntityId">Entity Id.</param>
        /// <param name="p_lFolderTypeId">Folder Type Id.</param>         
        public static void AddCorrespondenceConnector(List<ConnectorCorrespondence> p_oConnectorCorrespondenceToAdd, long p_lConnectorId, long p_lEntityId, long p_lFolderTypeId)
        {
            try
            {
                //Variable declaration.
                var db = DbHelper.DataBase;

                //Log.
                NetBay.Core.Log.LogManager.Instance().LogDebug("DA_Connector.AddCorrespondenceConnector (p_lEntityId <" + p_lEntityId + ">, p_lFolderTypeId <" + p_lFolderTypeId + ">, p_lConnectorId <" + p_lConnectorId + ">, Nombre de correspondances du connecteur: p_oConnectorCorrespondenceToAdd.Count() <" + p_oConnectorCorrespondenceToAdd.Count + ">)");

                //Command declaration.
                foreach (ConnectorCorrespondence l_oConnectorCorrespondenceItem in p_oConnectorCorrespondenceToAdd)
                {
                    var dbCommand = db.GetNewCommand(DbFunction.AddCorrespondenceConnector.szGetDescription(), false);
                    db.AddInParameter(dbCommand, "p_lErpConnId", DbType.Int64, p_lConnectorId);
                    db.AddInParameter(dbCommand, "p_lEntityId", DbType.Int64, p_lEntityId);
                    db.AddInParameter(dbCommand, "p_lFolderTypeId", DbType.Int64, p_lFolderTypeId);
                    db.AddInParameter(dbCommand, "p_lErpCorrIndexPos", DbType.Int64, l_oConnectorCorrespondenceItem.ConnectorIndexCorrespondencePosition);
                    db.AddInParameter(dbCommand, "p_sErpCorrDescription", DbType.String, l_oConnectorCorrespondenceItem.ConnectorIndexCorrespondenceDescription);

                    //Execute Stored procedure.
                    db.ExecuteNonQuery(dbCommand);
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Connector.AddCorrespondenceConnector Exception [" + ex.Message + "]");
            }
        }

        /// <summary>Add RIO connector parameters.</summary>
        /// <param name="p_oConnectorParametersToAdd">Connector parameters Object.</param>
        /// <param name="p_lConnectorId">Connector Id.</param>
        public static void AddParameterConnector(List<ConnectorParameters> p_oConnectorParametersToAdd, long p_lConnectorId)
        {
            try
            {
                //Variable declaration.
                var db = DbHelper.DataBase;

                //Log.
                NetBay.Core.Log.LogManager.Instance().LogDebug("DA_Connector.AddParameterConnector (p_lConnectorId <" + p_lConnectorId + ">, Nombre de paramètres du connecteur: p_oConnectorParametersToAdd.Count() <" + p_oConnectorParametersToAdd.Count + ">)");
              
                //Command declaration.
                foreach (ConnectorParameters l_oConnectorParametersItem in p_oConnectorParametersToAdd)
                {
                    var dbCommand = db.GetNewCommand(DbFunction.AddParametersConnector.szGetDescription(),false);
                    db.AddInParameter(dbCommand, "p_lErpConnId", DbType.Int64, p_lConnectorId);
                    db.AddInParameter(dbCommand, "p_sErpParamRIO", DbType.String, l_oConnectorParametersItem.ConnectorRioParam);
                    db.AddInParameter(dbCommand, "p_sErpParamERP", DbType.String, l_oConnectorParametersItem.ConnectorCustomerParam);
                    db.AddInParameter(dbCommand, "p_sErpParamDescription", DbType.String, l_oConnectorParametersItem.ConnectorDescriptionParam);

                    //Execute Stored procedure.
                    db.ExecuteNonQuery(dbCommand);
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Connector.AddParameterConnector Exception [" + ex.Message + "]");
            }
        }

        #endregion Added

        #region Delete

        /// <summary>Delete RIO connector.</summary>
        /// <param name="p_lConnectorId">Connector Id.</param> 
        public static void DeleteConnectorByConnectorId(long p_lConnectorId)
        {
            try
            {
                //Variable declaration.
                var db = DbHelper.DataBase;

                //Log.
                NetBay.Core.Log.LogManager.Instance().LogDebug("DA_Connector.DeleteConnectorByConnectorId (p_lConnectorId <" + p_lConnectorId + ">)");

                //Command declaration.
                var dbCommand = db.GetNewCommand(DbFunction.DeleteConnectorByConnectorID.szGetDescription(), false);
                db.AddInParameter(dbCommand, "p_lErpConnId", DbType.Int64, p_lConnectorId);

                //Execute Stored procedure.
                db.ExecuteNonQuery(dbCommand);

            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Connector.DeleteConnectorByConnectorId Exception [" + ex.Message + "]");
            }
        }

        /// <summary>Delete List RIO connector.</summary>
        /// <param name="p_lConnectorId">List Connector Id.</param> 
        public static void DeleteConnectorByListConnectorId(string p_sConnectorListId)
        {
            try
            {
                //Variable declaration.
                var db = DbHelper.DataBase;
                string[] l_aConnectorId = p_sConnectorListId.Split(new[] { ';' });

                //Log.
                NetBay.Core.Log.LogManager.Instance().LogDebug("DA_Connector.DeleteConnectorByListConnectorId (p_lConnectorId <" + p_sConnectorListId + ">)");

                foreach (string l_sConnectorId in l_aConnectorId)
                {
                    //Command declaration.
                    var dbCommand = db.GetNewCommand(DbFunction.DeleteConnectorByConnectorID.szGetDescription(), false);
                    db.AddInParameter(dbCommand, "p_lErpConnId", DbType.Int64, Convert.ToInt64(l_sConnectorId));

                    //Execute Stored procedure.
                    db.ExecuteNonQuery(dbCommand);
                }

            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Connector.DeleteConnectorByListConnectorId Exception [" + ex.Message + "]");
            }
        }

        #endregion

        #region Update

        /// <summary>Update RIO connector.</summary>
        /// <param name="p_lConnectorId">Connector Id.</param>   
        /// <param name="p_sConnectorDescription">Connector description.</param>
        /// <param name="p_sConnectorName">Connector name.</param>
        public static long UpdateNameAndDescriptionConnectorByConnectorId(long p_lConnectorId, string p_sConnectorName, string p_sConnectorDescription)
        {
            //Variable initialization.
            long l_lConnectorId = 0;

            try
            {
                //Variable declaration.
                var db = DbHelper.DataBase;

                //Log.
                NetBay.Core.Log.LogManager.Instance().LogDebug("DA_Connector.UpdateNameAndDescriptionConnectorByConnectorId (p_lConnectorId <" + p_lConnectorId + ">, p_sConnectorName <" + p_sConnectorName + ">, p_sConnectorDescription <" + p_sConnectorDescription + ">)");

                //Command declaration.
                using (var dbCommand = db.GetNewCommand(DbFunction.UpdateConnectorByConnectorID.szGetDescription()))
                {
                    db.AddInParameter(dbCommand, "p_lErpConnId", DbType.Int64, p_lConnectorId);
                    db.AddInParameter(dbCommand, "p_sErpConnName", DbType.String, p_sConnectorName);
                    db.AddInParameter(dbCommand, "p_sErpConnDesc", DbType.String, p_sConnectorDescription);

                    //Execute Stored procedure.
                    using (IDataReader P_oDbDataReader = db.ExecuteReader(dbCommand))
                    {

                        while (P_oDbDataReader.Read())
                        {
                            l_lConnectorId = Convert.ToInt64(P_oDbDataReader["RECON_ID"]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Connector.UpdateNameAndDescriptionConnectorByConnectorId Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_lConnectorId;
        }

        #endregion Update
    }
}
